package com.nervenets.web.redis.domain.global;

import com.nervenets.web.enumeration.Time;
import com.nervenets.web.redis.RedisDomain;
import com.nervenets.web.redis.domain.user.UserStored;

import java.util.HashSet;

/**
 * 用户数量统计
 * Created by Joe on 2018/1/9.
 */
public class GlobalUserCount extends RedisDomain {
    private Time timeType;
    private int timestamp;
    private HashSet<Long> activeUsers = new HashSet<>();
    private HashSet<Long> newUsers = new HashSet<>();

    public GlobalUserCount(Time timeType, int timestamp) {
        this.timeType = timeType;
        this.timestamp = timestamp;
        setExpire(0x93a80);//保存7x24小时以便追述
    }

    public void addUser(UserStored user) {
        if (null != user) {
            activeUsers.add(user.getUserId());
            if (user.getCreateTime() > timestamp) {
                newUsers.add(user.getUserId());
            }
        }
    }

    @Override
    public String getKey() {
        return String.format("GLOBAL_USER_COUNT_%s_%s", timeType.getName().toUpperCase(), timestamp);
    }

    public Time getTimeType() {
        return timeType;
    }

    public void setTimeType(Time timeType) {
        this.timeType = timeType;
    }

    public int getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(int timestamp) {
        this.timestamp = timestamp;
    }

    public HashSet<Long> getActiveUsers() {
        return activeUsers;
    }

    public void setActiveUsers(HashSet<Long> activeUsers) {
        this.activeUsers = activeUsers;
    }

    public HashSet<Long> getNewUsers() {
        return newUsers;
    }

    public void setNewUsers(HashSet<Long> newUsers) {
        this.newUsers = newUsers;
    }
}
